package com.gouying.web.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gouying.web.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author ly
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    /**
     * 在支付宝创建的应用的id
     */
    private String app_id = "2021000117658475";

    /**
     * 商户私钥，您的PKCS8格式RSA2私钥
     */
    private String merchant_private_key = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQC2YaCMaW+sMt3DNv17Y3ihXprbyC87abs99mA0KCblcSCYVlIPPYgGRmX/yRhxDYZeqKFrgKZC1BsFYXvhDUGDnjBiRP50N+7zwnFKHEwhSI4NGMdUiDPG0ZA9RhTZeGehy0hxbq6EORjujuzKyMn4pucsVIvdPuMHa37hxGYQEr5cWFw5PgPCjJG7+0bNdXiiX7Z1Ox/ixzzt1nT61TrVJccePwb2DoyrSwgm74lUTZkhkJMAGeXkVyO4bJ5lW5R8bSWONvTGhdXBkDirRdifdO/ktm5TXWqF0yisaS1X/3tXiABroXBE1r+Hrq+zROwMbfxDAgDpZbxhRDpbGdhjAgMBAAECggEBAKFujqZaWT9pksNNdFb4N/lV/OqXKb+EGfmBj6v68lrapIhSt7XHuBQd0gDzZdADsbEtwBugtwmmJvi/TeBpVFkE9zy0ZypFrBtVO+PmYjh/uUS8bwtSt8m/CtZBpwjLO6eJgOjumEhvJbNbDRt9WNDgIvUpLNe750ZVDu0VY2XLQAjATUKyivFeiMfnM/zdWJ3a45vkciQOVe+dBArqmfR7IHKG7D4E0cvmcRVtOtl0hYFGsZulO4lPv6PgmedKCXrpVpgB+Yt9+zb56yPdLaod9falPosy/Ge0p6NevGUqD8xNeSTg7MClR4perwGe4F7i1geT8UUDj3wDy+r1kWkCgYEA9GcChNBCuROeZmZYF2QdL/28M6iFApzxPCIG2HGdXPtruhRuxgjVQS4Tq7H2UCcqmfo9Slh4fB57Mv0vuo1qSNF+Bb2N30POqnePH6IJ6r76WQqWK/0KxdMCF2LQ8ZlHOvZlPytwsaVmFQa41E37VmFYMyaCOOzFfxDLPqPoQ+8CgYEAvwkwO55YhS3xITsh4/OTPsQEofCDwrjw4iW3DLRL0H3c9p3qcAcsMFLBtlE1VCMrltVCHypp53/8Miutqd53JEoukJzTde44jcx1qANOTrZgA19Dn/xeO+JWNaEa3ejWH70NAC4cDr6kAL3pyjfvN6uOb5kyMMs+dwNg9ogBrs0CgYEApS29iQ/MGJPG2YRQAzcZdubxPKSniLWTYMXKhJx882vFShUnLNapcoz2pHL5ewCNjVWP+OBERfa/4x01FcsfyykzGCQdb4i/c3yskX+NXkdGoclC52X00vCimDWj67Vha9s9YG02lAmqLYnGf05ZEO9nkeJz3o8/h3pNMv/KKacCgYEAgoiXwZUQLWhYBHnwTV9JNLEh7Tl3/tz6E7zrmC22RC9MheMfpw7vndFedeKFiwlBJp5vwPXtSXcFb46y6oiNZmmd6ciPIVz2ncWge35OyrAJaSR2A+KgzVB50clUn18SuLsoIABhwZ14WN1r23Ze6e46tHy8c+TcuNvBLJOAet0CgYEAyGIXJSRj67/Sbgz7wDLDLpZb4tCfuR3spQUtu4/4irNUHPl/6H0L+zw1T7/V3yvGv6jwRud2twknzSvr1zEuI0hF95oLNjvxQZkS88G6VmC6DFQcQL3u6SKZzBf5ZWbiuz6ajYspfc3M9ATPtdDYCHxYYQXvDcryCSScFWGSJf4=";
    /**
     * 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
     */
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh9l39jpVdZSs7DBBo3THvW+Y6iyKbFxNsBh74aLQEVPDV6Cgr6dCQvqU62SDOX2DNPBzrWmOkDoq0VWREcVrKz0b4oYRg0Gl7Dy1KBHG1UM8beGHh4jwQT1bLHrI6JgJ52FhGaPcK0nLJk+Lai6yZZpS1dATxkE9v8XqqZFSC4uZcUE3yp7EujwXdLLVe+z/oVP18jV58vPnEis1+LLFRPiUcFKPWyvhEa0/n0QupuTvk+kSlscWGuKH5wIuuNLHP44aEWUaXT9lujInOSP/0bmwhLq/sczh16MyPI8MicnDA5wpzkGW2OH9N1ImeEz4ucSSBH8qWXALkw/4+7lKyQIDAQAB";
    /**
     * 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     * 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
     */
    private String notify_url = "http://3p9x702438.qicp.vip/payed/notify";

    /**
     * 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     * 同步通知，支付成功，一般跳转到成功页
     */
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    /**
     * 签名方式
     */
    private String sign_type = "RSA2";

    /**
     * 字符编码格式
     */
    private String charset = "utf-8";

    /**
     * 自动收单超时时间
     */
    private String timeout = "30m";

    /**
     * 支付宝网关； https://openapi.alipaydev.com/gateway.do
     */
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                //一分钟不结算就算超时
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);
        return result;
    }
}
